﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using THO7AlgorithmBase;

namespace THO7AlgoritmTimerApplication
{
    class BlackOutAlgorithm : VisionAlgorithm
    {
        public BlackOutAlgorithm(String name) : base(name) { }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Bitmap returnImage = new Bitmap(sourceImage);

            Rectangle rectangle = new Rectangle(0, 0, returnImage.Width, returnImage.Height);
            BitmapData bitmapData =
                returnImage.LockBits(rectangle, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                returnImage.PixelFormat);

            IntPtr ptr = bitmapData.Scan0;

            int size = Math.Abs(bitmapData.Stride) * sourceImage.Height;
            byte[] rgbValues = new byte[size];

            Marshal.Copy(ptr, rgbValues, 0, size);
            byte blackByte = Convert.ToByte(0);
            for (int i = 0; i < rgbValues.Length - 4; i = i + 4)
            {
                rgbValues[i] = blackByte;
                rgbValues[i + 1] = blackByte;
                rgbValues[i + 2] = blackByte;
            }
                
            Marshal.Copy(rgbValues, 0, ptr, size);
            returnImage.UnlockBits(bitmapData);
            return returnImage;
        }
    }
}
